<?php  
// Main ReciveMail Class File - Version 1.0 (03-06-2015)  
/* 
 * File: recivemail.class.php 
 * Description: Reciving mail With Attechment 
 * Version: 1.1 
 * Created: 03-06-2015 
 * Author: Sara Zhou 
 */
namespace  addons\qingdongams\library;

class ReceiveMail
{  
    var $server='';  
    var $username='';  
    var $password='';
      
    var $marubox='';                      
      
    var $email='';        
      
    function __construct($username,$password,$EmailAddress,$mailserver='localhost',$servertype='pop',$port='110',$ssl = false) //Constructure
    {  
        if($servertype=='imap')  
        {  
            if($port=='') $port='143';   
            $strConnect='{'.$mailserver.':'.$port. '}INBOX';   
        }  
        else  
        {  
            $strConnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';
//            $strConnect="{pop.163.com:110/pop3}INBOX";
        }
        $this->server            =   $strConnect;  
        $this->username          =   $username;  
        $this->password          =   $password;  
        $this->email         =   $EmailAddress;  
    }  
    function connect() //Connect To the Mail Box  
    {
        ini_set("display_errors", "On");//打开错误提示
        ini_set("error_reporting",E_ALL);//显示所有错误
        $this->marubox=@imap_open($this->server,$this->username,$this->password);
        if(!$this->marubox)  
        {  
            return false;  
//          echo "Error: Connecting to mail server";  
//          exit;  
        }  
        return true;  
    }  
      
      
    function getHeaders($mid) // Get Header info  
    {  
        if(!$this->marubox)  
            return false;  
  
        $mail_header=imap_header($this->marubox,$mid);
        $sender=$mail_header->from[0];  
        $sender_replyto=$mail_header->reply_to[0];  
//        if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster')  // 退信的发件人是postmaster哦

        $subject=$this->decode_mime($mail_header->subject);
        $ccList=array();
        if(isset($mail_header->cc)){
            foreach ($mail_header->cc as $k => $v)
            {
                $ccList[]=$v->mailbox.'@'.$v->host;
            }
        }
        $toList=array();
        if(isset($mail_header->to)){
           foreach ($mail_header->to as $k => $v)
           {
               $toList[]=$v->mailbox.'@'.$v->host;
           }
        }
        $ccList=implode(",", $ccList);
        $toList=implode(",", $toList);
        $mail_details=array(
                'fromBy'=>strtolower($sender->mailbox).'@'.$sender->host,
                'fromName'=>isset($sender->personal) ? $this->decode_mime($sender->personal) : $sender->mailbox,
                'ccList'=>$ccList,//strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
                'toNameOth'=>isset($sender_replyto->personal) ? $this->decode_mime($sender_replyto->personal) : $sender_replyto->mailbox,  //转换邮件标题的字符编码,处理乱码（猜测应该是如果要回复邮箱，回复人）
                'subject'=>$subject,
                'mailDate'=>date("Y-m-d H:i:s",$mail_header->udate),
                'udate'=>$mail_header->udate,
                'toList'=>$toList//strtolower($mail_header->to[0]->mailbox).'@'.$mail_header->to[0]->host
//                  'to'=>strtolower($mail_header->toaddress)  
            );

        return $mail_details;  
    }  
    function get_mime_type(&$structure) //Get Mime type Internal Private Use  
    {   
        $primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");   
          
        if($structure->subtype && $structure->subtype!="PNG") {   
            return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype;   
        }   
        return "TEXT/PLAIN";   
    }   
    function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) //Get Part Of Message Internal Private Use  
    {
        if(!$structure) {   
            $structure = imap_fetchstructure($stream, $msg_number);   
        }
        if($structure) {
            if($mime_type == $this->get_mime_type($structure))
            {   
                if(!$part_number)   
                {   
                    $part_number = "1";   
                }   
                $text = imap_fetchbody($stream, $msg_number, $part_number);
                if($structure->encoding == 3)  
                {
                  if ($structure->parameters[0]->value!="utf-8")
                  {
                      return iconv('GB2312','UTF-8//IGNORE',imap_base64($text));
                  }
                  else
                  {
                      return iconv('GB2312','UTF-8//IGNORE',imap_base64($text));
                  }
                }  
                else if($structure->encoding == 4)  
                {
                    if ($structure->parameters[0]->value =="utf-8" || $structure->parameters[0]->value =='UTF-8')
                    {
                        return imap_qprint($text);
                    }
                    else
                    {
                        echo iconv('GB2312','UTF-8//IGNORE',imap_base64($text));
                    }
                }  
                else  
                {
                    return iconv('GB2312','UTF-8//IGNORE',$text);
                }  
            }

            if($structure->type == 1) /* multipart */   
            {   
                while(list($index, $sub_structure) = $this->fun_adm_each($structure->parts))
                {
                    $prefix = '';
                    if($part_number)  
                    {   
                        $prefix = $part_number . '.';   
                    }   
                    $data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));   
                    if($data)  
                    {
                        return $data;
                    }   
                }   
            }   
        }  
        return false;   
    }
    function fun_adm_each(&$array)
    {
        $res = array();
        $key = key($array);
        if ($key !== null) {
            next($array);
            $res[1] = $res['value'] = $array[$key];
            $res[0] = $res['key'] = $key;
        } else {
            $res = false;
        }
        return $res;
    }
    function getTotalMails() //Get Total Number off Unread Email In Mailbox  
    {  
        if(!$this->marubox)  
            return false;  
  
//      return imap_headers($this->marubox);  
        return imap_num_recent($this->marubox);  
    }  
      
    function GetAttach($mid,$path) // Get Atteced File from Mail  
    {
        if(!$this->marubox)  
            return false;  
  
        $struckture = imap_fetchstructure($this->marubox,$mid);  // 获取某信件的结构信息
          
        $files=array();
        if(isset($struckture->parts))
        {
            foreach($struckture->parts as $key => $value)  
            {  
                $enc=$struckture->parts[$key]->encoding;  
                  
                //取邮件附件  
                if($struckture->parts[$key]->ifdparameters)  
                {
                    //命名附件,转码  
                    $name=$this->decode_mime($struckture->parts[$key]->dparameters[0]->value);                    
                    $extend =explode("." , $name);  
                    $file['extension'] = $extend[count($extend)-1];  
                    $file['pathname']  = $this->setPathName($key, $file['extension']);  
                    $file['title']     = !empty($name) ? htmlspecialchars($name) : str_replace('.' . $file['extension'], '', $name);  
                    $file['size']      = isset($struckture->parts[$key]->dparameters) ? $struckture->parts[$key]->dparameters[0]->value : 0;
                    $file['tmpname']   = $struckture->parts[$key]->dparameters[0]->value;
                    if(@$struckture->parts[$key]->disposition=="ATTACHMENT")  
                    {  
                        $file['type']      = 1;       
                    }  
                    else  
                    {  
                        $file['type']      = 0;  
                    }             
                    $files[] = $file;                     
                      
                    $message = imap_fetchbody($this->marubox,$mid,$key+1);  
                    if ($enc == 0)  
                        $message = imap_8bit($message);  
                    if ($enc == 1)  
                        $message = imap_8bit ($message);  
                    if ($enc == 2)  
                        $message = imap_binary ($message);  
                    if ($enc == 3)//图片  
                        $message = imap_base64 ($message);   
                    if ($enc == 4)  
                        $message = quoted_printable_decode($message);  
                    if ($enc == 5)  
                        $message = $message;
                    $fp=fopen($path.$file['pathname'],"w");  
                    fwrite($fp,$message);  
                    fclose($fp);  
                      
                }  
                // 处理内容中包含图片的部分
                if(isset($struckture->parts) && isset($struckture->parts[$key]->parts)){
                    if($struckture->parts[$key]->parts)
                    {
                        foreach($struckture->parts[$key]->parts as $keyb => $valueb)
                        {
                            $enc=$struckture->parts[$key]->parts[$keyb]->encoding;
                            if($struckture->parts[$key]->parts[$keyb]->ifdparameters)
                            {
                                //命名图片
                                $name=$this->decode_mime($struckture->parts[$key]->parts[$keyb]->dparameters[0]->value);
                                $extend =explode("." , $name);
                                $file['extension'] = $extend[count($extend)-1];
                                $file['pathname']  = $this->setPathName($key, $file['extension']);
                                $file['title']     = !empty($name) ? htmlspecialchars($name) : str_replace('.' . $file['extension'], '', $name);
                                $file['size']      = $struckture->parts[$key]->parts[$keyb]->dparameters[0]->value;
//                          $file['tmpname']   = $struckture->parts[$key]->dparameters[0]->value;
                                $file['type']      = 0;
                                $files[] = $file;

                                $partnro = ($key+1).".".($keyb+1);

                                $message = imap_fetchbody($this->marubox,$mid,$partnro);
                                if ($enc == 0)
                                    $message = imap_8bit($message);
                                if ($enc == 1)
                                    $message = imap_8bit ($message);
                                if ($enc == 2)
                                    $message = imap_binary ($message);
                                if ($enc == 3)
                                    $message = imap_base64 ($message);
                                if ($enc == 4)
                                    $message = quoted_printable_decode($message);
                                if ($enc == 5)
                                    $message = $message;
                                $fp=fopen($path.$file['pathname'],"w");
                                fwrite($fp,$message);
                                fclose($fp);
                            }
                        }
                    }
                }
            }  
        }  
        //move mail to taskMailBox  
        $this->move_mails($mid, $this->marubox);        
  
        return $files;  
    }  
      
    function getBody($mid,&$path,$imageList) // Get Message Body  
    {  
        if(!$this->marubox)  
            return false;  
  
        $body = $this->get_part($this->marubox, $mid, "TEXT/HTML");  
        if ($body == "")  
            $body = $this->get_part($this->marubox, $mid, "TEXT/PLAIN");  
        if ($body == "") {   
            return "";  
        }  
        //处理图片  
        $body=$this->embed_images($body,$path,$imageList);  
        return $body;  
    }  
      
    function embed_images(&$body,&$path,$imageList)  
    {  
        // get all img tags  
        preg_match_all('/<img.*?>/', $body, $matches);  
        if (!isset($matches[0])) return;  
          
        foreach ($matches[0] as $img)  
        {  
            // replace image web path with local path  
            preg_match('/src="(.*?)"/', $img, $m);  
            if (!isset($m[1])) continue;  
            $arr = parse_url($m[1]);  
            if (!isset($arr['scheme']) || !isset($arr['path']))continue;  
              
//          if (!isset($arr['host']) || !isset($arr['path']))continue;  
            if ($arr['scheme']!="http")  
            {  
                $filename=explode("@", $arr['path']);
                if($imageList){
                    $body = str_replace($img, '<img alt="" src="'.$path.$imageList[$filename[0]].'" style="border: none;" />', $body);
                }

            }  
        }  
        return $body;  
    }  
      
    function deleteMails($mid) // Delete That Mail  
    {  
        if(!$this->marubox)  
            return false;  
          
        imap_delete($this->marubox,$mid);  
    }  
    function close_mailbox() //Close Mail Box  
    {  
        if(!$this->marubox)  
            return false;  
  
        imap_close($this->marubox,CL_EXPUNGE);  
    }  
      
    //移动邮件到指定分组  
    function move_mails($msglist,$mailbox)  
    {  
        if(!$this->marubox)
            return false;
        imap_mail_move($this->marubox, $msglist, json_encode($mailbox));
    }  
      
    function creat_mailbox($mailbox)  
    {  
        if(!$this->marubox)  
            return false;  
          
        //imap_renamemailbox($imap_stream, $old_mbox, $new_mbox);  
        imap_create($this->marubox, $mailbox);  
    }  
      
    /* 
     * decode_mime()转换邮件标题的字符编码,处理乱码 
     */  
    function decode_mime($str){
        $str=imap_mime_header_decode($str);
        if ($str[0]->charset!="default")  
        {
            return iconv($str[0]->charset,'utf8',$str[0]->text);
        }
        else  
        {  
            return $str[0]->text;  
        }  
    }  
      
    /** 
     * Set path name of the uploaded file to be saved. 
     * 
     * @param  int    $fileID 
     * @param  string $extension 
     * @access public 
     * @return string 
     */  
    public function setPathName($fileID, $extension)  
    {
        $destDir = 'public/emails/'.date('ymd');
        $absoluteDir = ROOT_PATH . $destDir;
        if (!is_dir($absoluteDir)) {
            @mkdir($absoluteDir, 0755, true);
        }
        return $destDir.'/'.date('ymd', time()) . $fileID . mt_rand(0, 10000) . '.' . $extension;
//        return date('Ym/dHis', time()) . $fileID . mt_rand(0, 10000) . '.' . $extension;
//        return 'public/emails/'.date('ymd', time()) . $fileID . mt_rand(0, 10000) . '.' . $extension;
    }
      
}  
?>